package com.c.ctgproject.adapter.guang

import android.view.View
import androidx.core.view.isVisible
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.c.ctgproject.adapter.ImageCommonRvV2Adapter
import com.c.ctgproject.base.BaseActivityV2
import com.c.ctgproject.databinding.ActivityCheckDetailBinding
import com.c.ctgproject.entity.guang.CheckDetailEntity
import com.c.ctgproject.event.PositionEvent
import com.c.ctgproject.http.ApiMethods
import com.c.ctgproject.http.MyObserverHandlerLoading
import com.c.ctgproject.utils.*
import com.c.ctgproject.wiget.common.CarInfoLayout
import com.c.ctgproject.wiget.common.CommonInputDialog
import com.c.ctgproject.wiget.dialog.CheckInputNameDialog
import org.greenrobot.eventbus.EventBus

/**
 *作者:LiBW
 *创建日期: 2021/10/13
 *描述: 审批详情 页面
 */
class CheckDetailActivity : BaseActivityV2(), CommonInputDialog.CommonInPutDialogCallBack, CheckInputNameDialog.CheckInputNameDialogCallback {
    private val mBinding by lazy { ActivityCheckDetailBinding.inflate(layoutInflater) }
    override fun getLayout() = mBinding.root
    private val stripId by lazy { intent.getIntExtra(MyConstants.ID, -1) }

    //拒绝理由
    private var inputDialog: CommonInputDialog? = null

    //物品明细
    private val mPartList = ArrayList<CheckDetailEntity.DataBean.ReleaseItemsBean>()
    private val mPartAdapter by lazy { OutMaterialDetailItemAdapter(this, mPartList) }

    //流程
    private val mFlowList = ArrayList<CheckDetailEntity.DataBean.HistoriesBean>()
    private val mFlowAdapter by lazy { CheckFlowAdapter(this, mFlowList) }

    private val mListPosition by lazy { intent.getIntExtra(MyConstants.POSITION, -1) }

    override fun event() {
        if (intent.getBooleanExtra(MyConstants.JUST_SHOW, false)) {
            mBinding.mBottomRL.visibility = View.GONE

        }

        ApiMethods.apiSubscribe(ApiMethods.retrofit.releaseStrip(stripId), MyObserverHandlerLoading(this, loading) {
            MyLog.d("放行条详情回调回调回调:$it")
            val data = getGson().fromJson(it, CheckDetailEntity::class.java).data


            //获取attr图片
            val imageUrls = ArrayList<String>()
            data?.attachments?.forEach { bean ->
                imageUrls.add(bean.attUrl.handlerNull())
            }
            if (imageUrls.isNotEmpty()) {
                mBinding.mAttrImageRV.visibility = View.VISIBLE
                mBinding.mAttrImageRV.adapter = ImageCommonRvV2Adapter(this, imageUrls)
                mBinding.mAttrImageRV.layoutManager = GridLayoutManager(this, 3, GridLayoutManager.VERTICAL, false)
            } else {
                mBinding.mAttrImageRV.visibility = View.GONE
            }

            //放行条明细
            data?.releaseItems?.let { list ->
                mPartList.addAll(list)
                mPartAdapter.notifyDataSetChanged()
            }
            //流程
            data?.histories?.let { list ->
                mFlowList.addAll(list)
                mFlowAdapter.notifyDataSetChanged()
            }
            data?.let { bean ->
                mBinding.mContainerLL.addView(CarInfoLayout(this, "放行单号", bean.bizNo.noDataHandler()))
                mBinding.mContainerLL.addView(CarInfoLayout(this, "携物单位", bean.carryingEnterprise.noDataHandler()))
                mBinding.mContainerLL.addView(CarInfoLayout(this, "携物人", bean.carrier.noDataHandler()))
                mBinding.mContainerLL.addView(CarInfoLayout(this, "车牌号", bean.plate.noDataHandler()))
                mBinding.mContainerLL.addView(CarInfoLayout(this, "提交人", bean.createPersonName.noDataHandler()))
                mBinding.mContainerLL.addView(CarInfoLayout(this, "提交时间", bean.createTime.noDataHandler()))
                mBinding.mContainerLL.addView(CarInfoLayout(this, "部门", bean.department.noDataHandler()))
                mBinding.mContainerLL.addView(CarInfoLayout(this, "审批人", bean.auditPersonName.noDataHandler()))
                mBinding.mContainerLL.addView(CarInfoLayout(this, "关联出库单", bean.outOrderInfo?.bizNo.noDataHandler()))
            }
        })


        mBinding.mFlowRV.adapter = mFlowAdapter
        mBinding.mFlowRV.layoutManager = LinearLayoutManager(this)
        mBinding.mPartListRV.adapter = mPartAdapter
        mBinding.mPartListRV.layoutManager = LinearLayoutManager(this)
    }

    override fun click() {
        //拒绝
        mBinding.mRefusedBTN.setOnClickListener {
            inputDialog = CommonInputDialog(this, "拒绝理由")
            inputDialog?.show(supportFragmentManager, "")
        }
        //同意
        mBinding.mConfirmBTN.setOnClickListener {
            CheckInputNameDialog(loading, this).show(supportFragmentManager, "")

        }
        //物品明细折叠开关
        mBinding.mPartListLL.setOnClickListener {
            if (mBinding.mPartListRV.isVisible) {
                mBinding.mArrowIV.handlerArrow(false, mBinding.mPartListRV)
            } else {
                mBinding.mArrowIV.handlerArrow(true, mBinding.mPartListRV)
            }
        }

    }

    //填写完的拒绝理由
    override fun dialogResultText(msg: String) {
        inputDialog?.dismiss()
        handlerHttp(2, msg = msg)
    }

    //1-通过 2-不通过
    private fun handlerHttp(status: Int, msg: String? = null, nameUrl: String? = null) {
//        val map = hashMapOf<String, Any?>()
//        map["status"] = status
//        if (status == 0) {
//            map["msg"] = msg
//        }
        ApiMethods.apiSubscribe(ApiMethods.retrofit.releaseStripCheck(stripId, status, msg, nameUrl), MyObserverHandlerLoading(this, loading) {
            MyLog.d("回调回调:$it")
            handlerHttpResult(it) {
                showToast("提交成功")
                EventBus.getDefault().post(PositionEvent(mListPosition))
                finish()
            }
        })
    }

    override fun onImageGet(url: String) {
        handlerHttp(1, nameUrl = url)
    }
}